//
//  Currying.swift
//  learnSwitf
//
//  Created by Open on 3/19/15.
//  Copyright (c) 2015 open-groupe. All rights reserved.
//

import Foundation
func addTwoNumber(a: Int)(num: Int) ->Int
{
    return a + num
}

protocol TargetAction
{
    func performAction()
}

struct TargetActionWrapper<T: AnyObject> : TargetAction {
    
    weak var target: T?
    let action: (T) -> () -> ()
    
    func performAction() -> () {
        if let t = target {
            action(t)()
        }
    }
}

enum ControlEvent {
    case TouchUpIndide
    case ValueChanged
    //...
}

class Control {
    var actions = [ControlEvent: TargetAction]()
    
    func setTarget<T: AnyObject>(target: T, action: (T) -> () -> (), controlEvent: ControlEvent ) {
        actions[controlEvent] = TargetActionWrapper(target: target, action: action)
    }
    
    func removeTargetForControlEvent(controlEvent: ControlEvent) {
        actions[controlEvent] = nil
    }
    
    func performActionForControlEvent(controlEvent: ControlEvent) {
        actions[controlEvent]?.performAction()
    }
}

//usage
class MyViewController {
    let button = Control()
    
    func viewDidLoad ()
    {
        button.setTarget(self, action: MyViewController.OnButtonTap, controlEvent: .TouchUpIndide)
        button.performActionForControlEvent(.TouchUpIndide)
    }
    
    func OnButtonTap() {
        println("button was tapped")
    }
    
}



